import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router"
import http from '@/utils/http'

const routes: Array<RouteRecordRaw> = [
    {
        path: "/login",
        component: () => import("@/views/login/index.vue"),
        meta: {
            title: "登录",
            noauth: true,
            noAlarmEvent: true
        },
    },
    {
        path: "/",
        component: () => import("@/views/layout-header.vue"),
        redirect: "/main",
        children: [
            {
                path: "main",
                component: () => import("@/views/main/basic.vue"),
                meta: {
                    title: "首页",
                    // 全景模式, 背景的底图有区别
                    panorama: true
                }
            },
            {
                path: "navigation",
                component: () => import("@/views/navigation/basic.vue"),
                meta: { title: "功能导航", navigator: true }
            },
            {
                path: "setting",
                component: () => import("@/views/layout-left.vue"),
                meta: {title: "系统管理"},
                children: [
                    {
                        path: "user",
                        meta: {title: "账户管理"},
                        children: [
                            {
                                path: "account",
                                component: () => import("@/views/setting/user/account.vue"),
                                meta: {title: "账户管理"}
                            },
                            {
                                path: "role",
                                component: () => import("@/views/setting/user/role.vue"),
                                meta: {title: "角色管理"}
                            }
                        ]
                    },
                    {
                        path: "system",
                        component: () => import("@/views/setting/system.vue"),
                        meta: {title: "系统设置"}
                    },
                    {
                        path: "banner",
                        component: () => import("@/views/setting/banner.vue"),
                        meta: {title: "banner管理"}
                    },
                    {
                        path: "fast",
                        component: () => import("@/views/setting/fast.vue"),
                        meta: {title: "快捷功能设置"}
                    }
                ]
            },
            {
                path: "service",
                component: () => import("@/views/layout-left.vue"),
                meta: {title: "服务中心"},
                children: [
                    {
                        path: "classify",
                        component: () => import("@/views/service/classify.vue"),
                        meta: {title: "服务分类管理"}
                    },
                    {
                        path: "project",
                        component: () => import("@/views/service/project.vue"),
                        meta: {title: "服务项目管理"}
                    }
                ]
            }
        ]
    },
    {
        path: "/operateRecord",
        component: () => import("@/views/record/operateRecord.vue"),
        meta: {
            title: "?",
        },
    }
]
const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

let pageInit = false

// 初次进入项目的时候预先执行的事件 - 获取登录态和权限信息
router.beforeResolve(async (to) => {
    // console.log('to', to)
    if (pageInit || to.meta && to.meta.noauth) {
        return true
    }
    //设置标志，表示已经初始化过，后续路由变化不再请求
    pageInit = true
    try {
        //浏览器登录  判断是否 进行了初始化配置
        //const res: any = await http.get('/auth/has', {})
        // if (res.code == 2001) {
        //     //没有进行初始化配置
        //     return '/auth/service'
        // }
    } catch (err) {
        return '/login'
    }
    // if (!sessionStorage.getItem('token')) {
    //     return '/login'
    // }
    if (!localStorage.getItem('token')) {
        return '/login'
    }
    return true
})
export default router
